//
//  FilterViewController.m
//  Rockval
//
//  Created by TuanNT on 4/28/14.
//  Copyright (c) 2014 TNL. All rights reserved.
//

#import "FilterViewController.h"
#import "UIViewController+Modal.h"
#import "SVGeocoder.h"
#import "ActionSheetPicker.h"

@interface FilterViewController ()
@property (weak, nonatomic) IBOutlet UIButton *buttonPriceMin;
@property (weak, nonatomic) IBOutlet UIButton *buttonPriceMax;
@property (weak, nonatomic) IBOutlet UIButton *buttonBuildingSizeMin;
@property (weak, nonatomic) IBOutlet UIButton *buttonBuildingSizeMax;
@property (weak, nonatomic) IBOutlet UIButton *buttonLotSizeMin;
@property (weak, nonatomic) IBOutlet UIButton *buttonLotSizeMax;

@property (nonatomic, strong) NSArray *searchTypes;
@property (nonatomic, strong) NSArray *propertyTypes;
@property (nonatomic, strong) NSArray *locations;
@property (nonatomic, strong) NSArray *prices;
@property (nonatomic, strong) NSArray *buildingSize;
@property (nonatomic, strong) NSArray *lotSize;
@property (nonatomic, strong) SVPlacemark *currentPlace;
@end

@implementation FilterViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
	self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	if (self) {
		// Custom initialization
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];
	// Do any additional setup after loading the view.
	[self getLocationInfo];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self.navigationController setToolbarHidden:YES animated:NO];
}

- (void)didReceiveMemoryWarning {
	[super didReceiveMemoryWarning];
	// Dispose of any resources that can be recreated.
}

/*
   #pragma mark - Navigation

   // In a storyboard-based application, you will often want to do a little preparation before navigation
   - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
   {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
   }
 */

//================================ GETTER ================================
#pragma mark -
#pragma mark GETTER

- (NSArray *)searchTypes {
	if (!_searchTypes) {
		_searchTypes = @[@"All type", @"Sale", @"Lease"];
	}
	return _searchTypes;
}

- (NSArray *)propertyTypes {
	if (!_propertyTypes) {
		_propertyTypes = @[@"All", @"200"];
	}
	return _propertyTypes;
}

- (NSArray *)locations {
	if (!_locations) {
		_locations = @[@"Near me"];
	}
	return _locations;
}

- (NSArray *)prices {
	if (!_prices) {
		_prices = @[@"100", @"200"];
	}
	return _prices;
}

- (NSArray *)buildingSize {
	if (!_buildingSize) {
		_buildingSize = @[@"10", @"20"];
	}
	return _buildingSize;
}

- (NSArray *)lotSize {
	if (!_lotSize) {
		_lotSize = @[@"10", @"20"];
	}
	return _lotSize;
}

//================================ NAVIGATION ITEMS ================================
#pragma mark -
#pragma mark NAVIGATION ITEMS

- (IBAction)onLeftButtonItem:(id)sender {
	DLog();
	if (![self isModal]) {
		[super onLeftButtonItem:sender];
		return;
	}
	if (self.cancelBlock) {
		dispatch_async(dispatch_get_main_queue(), ^{
		    self.cancelBlock();
		});
	}
}

- (void)onRightButtonItem {
	DLog();
	[[APIManager sharedInstance] filterByType:@"1"
	                                  keyword:@""
	                                 priceMin:self.buttonPriceMin.titleLabel.text
	                                 priceMax:self.buttonPriceMax.titleLabel.text
	                                  yearMin:@"2000"
	                                  yearMax:@"2015"
	                                  country:self.currentPlace.country
	                                    state:self.currentPlace.ISOcountryCode
	                                     city:self.currentPlace.subLocality
	                                      zip:self.currentPlace.postalCode
	                          buildingSizeMin:self.buttonBuildingSizeMin.titleLabel.text
	                          buildingSizeMax:self.buttonBuildingSizeMax.titleLabel.text
	                               lotSizeMin:self.buttonLotSizeMin.titleLabel.text
	                               lotSizeMax:self.buttonLotSizeMax.titleLabel.text
	                                    block: ^(NSArray *list) {
	    DLog(@"List = %@", list);
	}];
}

//================================ UITABLEVIEW ================================
#pragma mark -
#pragma mark UITABLEVIEW

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
	NSArray *data = nil;
	switch (indexPath.row) {
		case 0:
			data = self.searchTypes;
			break;

		case 1:
			data = self.propertyTypes;
			break;

		case 2:
			data = self.locations;
			break;

		default:
			break;
	}

	if (!data) {
		return;
	}
	[ActionSheetStringPicker showPickerWithTitle:nil
	                                        rows:data
	                            initialSelection:0
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    cell.detailTextLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

//================================ METHODS ================================
#pragma mark -
#pragma mark METHODS

- (void)getLocationInfo {
	[[RCLocationManager sharedManager] startUpdatingLocationWithBlock: ^(CLLocationManager *manager, CLLocation *newLocation, CLLocation *oldLocation) {
	    [SVGeocoder reverseGeocode:newLocation.coordinate completion: ^(NSArray *placemarks, NSHTTPURLResponse *urlResponse, NSError *error) {
	        if (placemarks.count == 0) {
	            return;
			}
	        _currentPlace = placemarks[0];
	        DLogWarn(@"currentPlace = %@", _currentPlace);
		}];
	    [[RCLocationManager sharedManager] stopUpdatingLocation];
	} errorBlock: ^(CLLocationManager *manager, NSError *error) {
	    DLog(@"Do nothing");
	}];
}

- (IBAction)buttonMinPrice_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Min price"
	                                        rows:self.prices
	                            initialSelection:0
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

- (IBAction)buttonMaxPrice_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Max price"
	                                        rows:self.prices
	                            initialSelection:self.prices.count - 1
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

- (IBAction)buttonMinBuildingSize_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Min building size"
	                                        rows:self.buildingSize
	                            initialSelection:0
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

- (IBAction)buttonMaxBuildingSize_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Max building size"
	                                        rows:self.buildingSize
	                            initialSelection:self.buildingSize.count - 1
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

- (IBAction)buttonMinLotSize_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Min lot size"
	                                        rows:self.lotSize
	                            initialSelection:0
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

- (IBAction)buttonMaxLotSize_TouchUpInside:(id)sender {
	UIButton *btn = (UIButton *)sender;
	[ActionSheetStringPicker showPickerWithTitle:@"Max lot size"
	                                        rows:self.lotSize
	                            initialSelection:self.lotSize.count - 1
	                                   doneBlock: ^(ActionSheetStringPicker *picker, NSInteger selectedIndex, id selectedValue) {
	    btn.titleLabel.text = selectedValue;
	} cancelBlock: ^(ActionSheetStringPicker *picker) {
	} origin:self.view.window];
}

@end
